//
//  GrowingTextViewController.m
//  etionUI
//
//  Created by wangjian on 5/27/13.
//
//

#import "GrowingTextViewController.h"

#define OFFSET_H 2

@implementation CGrowingTextViewController
{
    UIView *m_bgView;
    UIView *m_skinView;
    HPGrowingTextView *m_textView;
    
    id <CGrowingTextViewControllerDelegate> m_delegate;
    NSString *m_szText;
    CGRect m_frame;
}

- (id)initWithFrame:(CGRect)frame text:(NSString *)szText delegate:(id <CGrowingTextViewControllerDelegate>)delegate
{
    self = [super init];
    if (self)
    {
        m_delegate = delegate;
        m_frame = frame;
        m_szText = [szText retain];
    }
    return self;
}

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor clearColor];
    self.view.frame = m_frame;
    
    m_textView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(5, OFFSET_H, self.view.width-5, 40)];
    m_textView.delegate = self;
    m_textView.minNumberOfLines = 1;
    m_textView.maxNumberOfLines = 5;
    
    m_textView.returnKeyType = UIReturnKeyDone;
    m_textView.font = [UIFont systemFontOfSize:15.0f];
    m_textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0);
    m_textView.backgroundColor = [UIColor whiteColor];
    
    [m_textView.internalTextView setText:m_szText];
    m_textView.frame = CGRectMake(5, OFFSET_H, self.view.width-5, m_textView.internalTextView.contentSize.height);
    [self growingTextView:m_textView willChangeHeight:m_textView.internalTextView.contentSize.height];
    
    m_skinView = [[UIView alloc] initWithFrame:CGRectMake(self.view.width, self.view.height-m_textView.height-2*OFFSET_H, self.view.width, m_textView.height+2*OFFSET_H)];
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:m_skinView.bounds];
    bgView.image = [[UIImage imageNamed:@"MessageEntryBackground.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:22];
    bgView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [m_skinView addSubview:bgView];
    [bgView release];
    [m_skinView addSubview:m_textView];
    
    UIImageView *inputShellView = [[UIImageView alloc] initWithFrame:CGRectMake(m_textView.left, 0, m_textView.width, m_skinView.height)];
    inputShellView.image = [[UIImage imageNamed:@"MessageEntryInputField.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:22];
    inputShellView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [m_skinView addSubview:inputShellView];
    [inputShellView release];
    
    [self.view addSubview:m_skinView];
    
    m_bgView = [[UIView alloc] initWithFrame:self.view.bounds];
    m_bgView.backgroundColor = [UIColor blackColor];
    m_bgView.alpha = 0.0;
    [self.view insertSubview:m_bgView belowSubview:m_skinView];
    [m_bgView addGestureRecognizer:[[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgViewTap:)]autorelease]];
}

- (void)begainEdit
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNoti:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNoti:) name:UIKeyboardWillHideNotification object:nil];
    
    [UIView animateWithDuration:.3 animations:^{
        m_bgView.alpha = 0.5;
    } completion:^(BOOL finished) {
    }];
    [m_textView becomeFirstResponder];
}

- (void)endEditComplete:(void (^)())complete
{
    [UIView animateWithDuration:.3 animations:^{
        m_bgView.alpha = 0.0;
 //       m_skinView.frame = CGRectMake(0, self.view.height, self.view.width, m_textView.height);
    } completion:^(BOOL finished) {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        complete();
    }];
    [m_textView resignFirstResponder];
}

- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(CGFloat)height
{
    CGFloat diff = (m_skinView.frame.size.height - 2*OFFSET_H - height);
	CGRect r = m_skinView.frame;
    r.size.height -= diff;
    r.origin.y += diff;
	m_skinView.frame = r;
}

- (void)keyboardWillShowNoti:(NSNotification *)noti
{
    CGRect frame = [(NSValue *) [[noti userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    double duration = [(NSNumber *)[[noti userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSInteger curve = [(NSNumber *)[[noti userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    [UIView animateWithDuration:duration delay:0 options:curve animations:^{
        m_skinView.frame = CGRectMake(0, self.view.height-frame.size.height-m_skinView.height, m_skinView.width, m_skinView.height);
    } completion:^(BOOL finished){
    }];
}

- (void)keyboardWillHideNoti:(NSNotification *)noti
{
    CGRect frame = [(NSValue *) [[noti userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    double duration = [(NSNumber *)[[noti userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSInteger curve = [(NSNumber *)[[noti userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    [UIView animateWithDuration:duration delay:0 options:curve animations:^{
        m_skinView.frame = CGRectMake(self.view.right, m_skinView.top + frame.size.height, m_skinView.width, m_skinView.height);
    } completion:^(BOOL finished){
    }];
}

- (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView
{
    [m_delegate growingTextViewController:self didEndEdit:m_textView.internalTextView.text];
    return NO;
}

- (void)bgViewTap:(id)sender
{
    [m_delegate growingTextViewControllerCancelEdit:self];
}

- (void)dealloc
{
    [m_szText release];
    [m_bgView release];
    [m_textView release];
    [m_skinView release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

@end
